package com.xiaodimall.backsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaodimall.backsys.common.CascadeSelectionResult;
import com.xiaodimall.backsys.common.JsonResult;
import com.xiaodimall.backsys.mapper.GoodsCategoryMapper;
import com.xiaodimall.backsys.pojo.GoodsCategory;
import com.xiaodimall.backsys.service.GoodsCategoryService;
import com.xiaodimall.backsys.utils.GoodsCategoryUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: CJJ
 * @Date: Created in 2022/11/27 19:36
 * @Description:
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<CascadeSelectionResult> getSelectionData() {
        //获取 1 级分类
        List<CascadeSelectionResult> categorySelResult = GoodsCategoryUtils
                .goodsCategoryToCascadeSelectionResult(
                        goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategory>().eq("category_level", 1))
                );
        // 根据 1 级分类的 id 给二级分类赋值
        for (CascadeSelectionResult cascadeSelectionResult : categorySelResult) {
            cascadeSelectionResult.setChildren(GoodsCategoryUtils
                    .goodsCategoryToCascadeSelectionResult(
                            goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategory>().eq("parent_id", cascadeSelectionResult.getValue()))
                    )
            );
        }
        // 三级分类赋值
        for (CascadeSelectionResult cascadeSelectionResult : categorySelResult) {
            for (CascadeSelectionResult child : cascadeSelectionResult.getChildren()) {
                child.setChildren(GoodsCategoryUtils
                        .goodsCategoryToCascadeSelectionResult(
                                goodsCategoryMapper.selectList(new QueryWrapper<GoodsCategory>().eq("parent_id", child.getValue()))
                        )
                );
            }
        }
        return categorySelResult;
    }

    @Override
    public JsonResult getGoodsCategoryByLevel(Integer goodsCategoryLevel) {
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_level",goodsCategoryLevel);
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectList(queryWrapper);
        return new JsonResult(200,"",goodsCategories);
    }

    @Override
    public JsonResult addGoodsCategory(GoodsCategory goodsCategory) {
        goodsCategory.setIsDeleted(0).setCreateTime(new Date());
        int insert = goodsCategoryMapper.insert(goodsCategory);
        if (insert != 1){
            return new JsonResult(201,"添加失败,服务器错误");
        }
        return new JsonResult(200,"添加成功");
    }

    @Override
    public JsonResult deleteGoodsCategoryById(Integer categoryId) {
        int i = goodsCategoryMapper.deleteById(categoryId);
        if (i != 1){
            return new JsonResult(201);
        }
        return new JsonResult(200);
    }

    @Override
    public JsonResult updateById(Integer categoryId, String categoryName, Integer categoryRank) {
        GoodsCategory category = goodsCategoryMapper.selectById(categoryId);
        category.setCategoryName(categoryName).setCategoryRank(categoryRank).setUpdateTime(new Date());
        int row = goodsCategoryMapper.updateById(category);
        if (row != 1){
            return new JsonResult(201);
        }
        return new JsonResult(200);
    }
}
